version: "3.2"

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: "mindsdb"
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"
      PGPORT: 15432
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 1s
      timeout: 1s
      retries: 15

  migrations:
    command: bash -c "cd /mindsdb && python ./mindsdb/migrations/migrate.py"

    <<: &globalSettings
      depends_on:
        db:
          condition: service_healthy
      build:
        context: ../
        dockerfile: ./docker/mindsdb.Dockerfile
      volumes:
        - type: bind
          source: ../
          target: /mindsdb
      environment:
        MINDSDB_STORAGE_DIR: "/mindsdb/var"
        MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
  mindsdb:
    # Copy global settings from migration above
    <<: *globalSettings
    restart: always
    ports:
      - '47334:47334'
      - '47335:47335'
      - '47336:47336'
        # command: bash -c "cd /mindsdb && python -m mindsdb --api=http,mysql,mongodb"
    command: bash -c "cd /mindsdb && python -m mindsdb --api=http,mysql,mongodb --config=tests/integration_tests/flows/config/docker_config.json"
    environment:
      MINDSDB_EXECUTOR_SERVICE_HOST: "executor"
      MINDSDB_EXECUTOR_SERVICE_PORT: "50052"
      MINDSDB_DB_SERVICE_HOST: "db_service"
      MINDSDB_DB_SERVICE_PORT: "50050"
      MINDSDB_DOCKER_ENV: "True"
      MINDSDB_STORAGE_DIR: "/mindsdb/var"
      MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
      FLASK_DEBUG: "1"
      FLASK_ENV: "development"
      FLASK_APP: "/mindsdb/mindsdb/__main__.py"
      SEPARATE_MIGRATIONS: "1"
    depends_on:
      - migrations
      - postgres_db
      - mysql_db
      - maria_db
    healthcheck:
      test:  ["CMD", "curl", "-f", "http://localhost:47334/api/util/ping"]
      interval: 30s
      timeout: 4s
      retries: 100

  lightwood:
    build:
      context: ../
      dockerfile: ./docker/ml_handler.Dockerfile
    depends_on:
      - mindsdb
    volumes:
      - type: bind
        source: ../
        target: /mindsdb
    environment:
      # have to share mindsdb database, because it doesn't work without it
      REGISTRY_URL: "http://handler_discovery:5000"
      PORT: 50051
      HOST: "lightwood"
      MINDSDB_SERVICE_TYPE: "lightwood"
      MINDSDB_STORAGE_DIR: "/mindsdb/var"
      MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
      REGISTRY_URL: "http://handler_discovery:5000"
      MINDSDB_DB_SERVICE_HOST: "db_service"
      MINDSDB_DB_SERVICE_PORT: "50050"

  huggingface:
    build:
      context: ../
      dockerfile: ./docker/ml_handler.Dockerfile
    depends_on:
      - mindsdb
    volumes:
      - type: bind
        source: ../
        target: /mindsdb
    environment:
      # have to share mindsdb database, because it doesn't work without it
      PORT: 50053
      HOST: "huggingface"
      MINDSDB_SERVICE_TYPE: "huggingface"
      MINDSDB_STORAGE_DIR: "/mindsdb/var"
      MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
      REGISTRY_URL: "http://handler_discovery:5000"
      MINDSDB_DB_SERVICE_HOST: "db_service"
      MINDSDB_DB_SERVICE_PORT: "50050"

  db_service:
    build:
      context: ../
      dockerfile: ./docker/db_handler.Dockerfile
    depends_on:
      - mindsdb
    volumes:
      - type: bind
        source: ../
        target: /mindsdb
    environment:
      MINDSDB_STORAGE_DIR: "/mindsdb/var"
      MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
      PORT: "50050"

    healthcheck:
      test:  ["CMD", "curl", "-f", "http://localhost:5000"]
      interval: 120s
      timeout: 4s
      retries: 100

  executor:
    build:
      context: ../
      dockerfile: ./docker/executor.Dockerfile
    depends_on:
      - mindsdb
    environment:
      # have to share mindsdb database, because it doesn't work without it
      PORT: 50052
      MINDSDB_STORAGE_DIR: "/mindsdb/var"
      MINDSDB_DB_CON: "postgresql://postgres:postgres@db:15432/mindsdb"
      MINDSDB_DB_SERVICE_HOST: "db_service"
      MINDSDB_DB_SERVICE_PORT: "50050"
      REGISTRY_URL: "http://handler_discovery:5000"
    volumes:
      - type: bind
        source: ../
        target: /mindsdb
    healthcheck:
      test:  ["CMD", "curl", "-f", "http://localhost:5000"]
      interval: 120s
      timeout: 4s
      retries: 100

  handler_discovery:
    build:
      context: .
      dockerfile: ./handler_discovery.Dockerfile
    environment:
      PORT: 5000

  postgres_db:
    image: "mindsdb/postgres-handler-test"
    environment:
      POSTGRES_PASSWORD: "supersecret"
    healthcheck:
      test: ["CMD", "sh -c 'pg_isready -U root -d test'"]
      interval: 10s
      timeout: 3s
      retries: 3

  maria_db:
    image: "mindsdb/mariadb-handler-test"
    environment:
      MARIADB_ROOT_PASSWORD: "supersecret"
    healthcheck:
      test: ["CMD", 'mysqladmin', 'ping', '-h', 'localhost', '-u', 'root', '-p$$MARIADB_ROOT_PASSWORD' ]
      interval: 10s
      timeout: 3s
      retries: 3

  mysql_db:
    image: "mindsdb/mysql-handler-test"
    command: "--secure-file-priv=/"
    environment:
      MYSQL_ROOT_PASSWORD: "supersecret"
      MYSQL_TCP_PORT: 13306
    healthcheck:
      test: ["CMD", 'mysqladmin', 'ping', '-h', 'localhost', '-u', 'root', '-p$$MYSQL_ROOT_PASSWORD' ]
      interval: 10s
      timeout: 3s
      retries: 3
